<?php
/*
 * @Description:
 * @Author: QinTianyu
 * @Date: 2020-04-24 11:18:02
 * @LastEditors: QinTianyu
 * @LastEditTime: 2020-05-07 10:26:28
 */
namespace app\finance\admin;
use app\system\admin\Admin;
use app\finance\model\Account as AccountModel;
use app\finance\model\OtherType as OtherTypeModel;
use think\Db;
use app\finance\model\SystemBalanceLog as SystemBalanceLogModel;
class OtherPay extends Admin
{
    // protected $hisiModel = 'SystemBalanceLog';//模型名称[通用添加、修改专用]
    protected function initialize()
    {
        parent::initialize();
        // if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->where('status',1)->all());
            $this->assign('accounts', $accounts);
            $other_types = OtherTypeModel::all();
            $this->assign('other_types', $other_types);
        // }
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = $data = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $account_id = $this->request->param('account_id/d', '');
            $cate       = $this->request->param('cate/d', '');
            $type      = $this->request->param('type', 1);
            $other_type      = $this->request->param('other_type', '');
            $where['is_effective'] = 1;
            if(!is_empty($account_id)){
                $where['account_id'] = $account_id;
            }
            if(!is_empty($other_type)){
                $where['other_type'] = $other_type;
            }
            if(!is_empty($cate)){
                $where['cate'] = $cate;
            }
            if($type == 2){
                $data['data'] = SystemBalanceLogModel::where($where)->where('type','in',[12,13])
                                ->field('*,type as type_text,account_id as account_name')
                                // ->page($page)->limit($limit)
                                ->order('id desc')->select();
                foreach ($data['data'] as $key => $value) {

                    if($value['cate'] == 1){
                        $data['data'][$key]['cate_text'] = '入账';
                    }else{
                        $data['data'][$key]['cate_text'] = '出账';
                    }
                    $data['data'][$key]['other_type_text'] = Db::name('other_type')->where('id',$value['other_type'])->value('name');
                }
                $data['count'] = SystemBalanceLogModel::where($where)->where('type','in',[12,13])->count();
                $data['code']   = 0;
                return json($data);
            }else{
                $data['data'] = SystemBalanceLogModel::where($where)->where('type','in',[12,13])
                                ->field('*,type as type_text,account_id as account_name')
                                ->page($page)->limit($limit)->order('id desc')->select();
                foreach ($data['data'] as $key => $value) {


                    $data['data'][$key]['other_type_text'] = Db::name('other_type')->where('id',$value['other_type'])->value('name');
                }
                $data['count'] = SystemBalanceLogModel::where($where)->where('type','in',[12,13])->count();
                $data['code']   = 0;
                return json($data);
            }


        }
        return $this->fetch();
    }
    public function getAccountBalance()
    {
        $id = $this->request->param('account_id');
        $AccountModel = new AccountModel;
        $balance = $AccountModel->getAccountBalance($id);
        echo $balance;die;
    }
    public function add()
    {
        if($this->request->isPost()){
            $postData = $this->request->post();
            if(is_empty($postData['account_id'])){
                return $this->error('账户不能为空');
            }
            if(is_empty($postData['type'])){
                return $this->error('类型不能为空');
            }
            if(is_empty($postData['price']) || $postData['price'] < 0){
                return $this->error('金额不能低于0');
            }
            if($postData['type'] == 12){
                $postData['cate'] = 2;
            }else{
                $postData['cate'] = 1;
            }
            if(!SystemBalanceLogModel::create($postData)){
                return $this->error('录入出错，请重试');
            }
            return $this->success('新增成功');

        }
        return $this->fetch('form');
    }
}
